package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.manufacture.R;
import com.lenovo.manufacture.net.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;

public class module4 extends BaseActivity {
    @BindView(R.id.m4_ll)
    LinearLayout m4Ll;

    List<Map<String, String>> personList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module4);
        ButterKnife.bind(this);
        async(data.getHrm(), v -> personList = v.getData());
        init();
        getData();
    }

    @SuppressLint("NewApi")
    private void getData() {
        for (int i = 0; i < 4; i++) {
            json.put("pos", i);
            int finalI = i;
            async(data.getpos(json), v -> {
                json.put("userLineId", v.getData().get(0).get("lineId"));
                async(data.getposition(json), v1 -> {
                    for (Map<String, String> datum : v1.getData()) {
                        if (!datum.get("userPersonId").equals("0")) {
                            System.out.println(finalI + "/" + Integer.valueOf(datum.get("positionId")));
                            TextView tv = getText(finalI, Integer.valueOf(datum.get("positionId")));
//                            Map<String, String> stringStringMap1 = personList.stream().filter(stringStringMap -> stringStringMap.get("id") == datum.get("userPersonId")).collect(Collectors.toList()).get(0);
                            Map<String, String> userPersonId = personList.get(Integer.valueOf(datum.get("userPersonId")));
                            tv.setText("姓名:" + userPersonId.get("personName") + '\n' + "体力:" + userPersonId.get("power"));
                            tv.setBackgroundColor(Color.parseColor("#82aaff"));
                            tv.setTag(true);
                        }
                    }
                });
            });
        }
    }

    private void init() {
        for (int i = 0; i < 4; i++) {
            for (int i1 = 0; i1 < 4; i1++) {
                int finalI = i;
                int finalI1 = i1;
                getText(i, i1).setOnClickListener(view -> {
                    if (view.getTag() != null) {
                        showMsg("岗位上有人");
                    } else {
                        json.put("userLineId", finalI);
                        json.put("type", finalI1);
                        async(data.addposition(json), v -> getData());
                    }
                });
            }
        }
    }

    private TextView getText(int x, int y) {
        return ((TextView) ((LinearLayout) ((RelativeLayout) m4Ll.getChildAt(x)).getChildAt(y)).getChildAt(0));
    }

}